package com.heima.article.controller.v1;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.apis.article.AuthorControllerApi;
import com.heima.article.service.ApAuthorService;
import com.heima.model.article.pojos.ApAuthor;
import com.heima.model.common.dtos.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @作者 itcast
 * @创建日期 2021/1/25 11:18
 **/
@RestController
@RequestMapping("/api/v1/author")
public class ApAuthorController implements AuthorControllerApi {
    @Autowired
    ApAuthorService apAuthorService;
    @GetMapping("/findByUserId/{id}")
    @Override
    public ApAuthor findByUserId(@PathVariable Integer id) {
        List<ApAuthor> list = apAuthorService.list(
                Wrappers.<ApAuthor>lambdaQuery()
                        .eq(ApAuthor::getUserId, id)
        );
        if(list !=null && !list.isEmpty()){
            return list.get(0);
        }
        return null;
    }
    @PostMapping("/save")
    @Override
    public ResponseResult save(@RequestBody ApAuthor apAuthor) {
        apAuthor.setCreatedTime(new Date());
        apAuthorService.save(apAuthor);
        return ResponseResult.okResult();
    }
    @GetMapping("/one/{id}")
    @Override
    public ApAuthor findById(@PathVariable Integer id) {
        return apAuthorService.getById(id);
    }
}
